#ifndef LOGGER_H
#define LOGGER_H

#include <iostream>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Category.hh>
#include <log4cpp/Priority.hh>
#include <string>

// 使用宏定义简化日志调用，自动添加文件名、行号和函数名
//警告级日志：一般需要关注的异常情况
#define LOG_WARN(msg) Logger::getInstance()->warn(__FILE__, __LINE__, __FUNCTION__, msg)
//错误级日志：影响程序正常运行的错误
#define LOG_ERROR(msg) Logger::getInstance()->error(__FILE__, __LINE__, __FUNCTION__, msg)
//调试级日志：开发阶段用于调试的详细信息
#define LOG_DEBUG(msg) Logger::getInstance()->debug(__FILE__, __LINE__, __FUNCTION__, msg)
//信息级日志:正常运行时的关键流程记录
#define LOG_INFO(msg) Logger::getInstance()->info(__FILE__, __LINE__, __FUNCTION__, msg)

// 用户操作相关的日志宏，添加用户ID信息
#define LOG_USER_REGISTER(uid, msg) Logger::getInstance()->info(__FILE__, __LINE__, __FUNCTION__, "User[" + std::to_string(uid) + "] register: " + msg)
//用户登陆日志
#define LOG_USER_LOGIN(uid, msg) Logger::getInstance()->info(__FILE__, __LINE__, __FUNCTION__, "User[" + std::to_string(uid) + "] login: " + msg)
//用户查看摄像头日志
#define LOG_USER_VIEW_CAMERA(uid, cameraId, msg) Logger::getInstance()->info(__FILE__, __LINE__, __FUNCTION__, "User[" + std::to_string(uid) + "] view camera[" + std::to_string(cameraId) + "]: " + msg)

class Logger {
public:
    //  /**
    //  * 不同级别日志的记录方法
    //  * @param file 调用日志的文件名
    //  * @param line 调用日志的行号
    //  * @param func 调用日志的函数名
    //  * @param msg 日志内容
    void warn(const char *file, int line, const char *func, const std::string &msg);
    void error(const char *file, int line, const char *func, const std::string &msg);
    void debug(const char *file, int line, const char *func, const std::string &msg);
    void info(const char *file, int line, const char *func, const std::string &msg);

    // 获取单例实例
    static Logger* getInstance();
    
    // 销毁单例实例
    static void destroy();
    
    // 初始化日志系统，设置日志文件路径
    bool init(const std::string &logFilePath);

private:
    // 单例模式：私有构造函数和析构函数
    Logger();
    ~Logger();
    
    // 禁用拷贝构造和赋值运算符
    Logger(const Logger &) = delete;
    Logger &operator=(const Logger &) = delete;
    
    // 单例实例指针
    static Logger* _pInstance;
    
    // 日志系统相关成员
    log4cpp::Category* _root;
    log4cpp::PatternLayout* _layout;
    log4cpp::FileAppender* _fileAppender;
    log4cpp::OstreamAppender* _consoleAppender;
    
    // 日志是否已初始化
    bool _isInitialized;
};

#endif
